﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace iPdf
{

    class SignPage : CertificatePage<SignOption>
    {
        #region Fields
        private CertifyLevel[] certifyLevels = new CertifyLevel[]
        {
            new CertifyLevel("None"),
            new CertifyLevel("No Changes Allowed"),
            new CertifyLevel("Form Filling"),
            new CertifyLevel("Form Filling and Annotations")
        };
        #endregion

        #region Properties
        public override bool ReadyToAct
        {
            get
            {
                return Certificate.Enabled 
                    && Certificate.IsValid &&
                    !string.IsNullOrEmpty(CertificatePassword.Text);
            }
        }

        protected override SignOption PageOption
        {
            get
            {
                SignOption signOpt = new SignOption();
                signOpt.Certificate = Certificate.DefaultCertificate;
                signOpt.Certificate.Password = CertificatePassword.Text;
                signOpt.VisibleSignature = VisibleSignature.Checked;
                signOpt.Reason = Reason.Text;
                signOpt.Contact = Contact.Text;
                signOpt.Location = Location.Text;
                signOpt.CertifyLevel = (CertifyLevel) this.CertifyLevel.SelectedItem;

                signOpt.Metadata = new PdfMetadata();
                signOpt.Metadata.Author = Author.Text;
                signOpt.Metadata.Title = Title.Text;
                signOpt.Metadata.Subject = Subject.Text;
                signOpt.Metadata.Keywords = Keywords.Text;
                signOpt.Metadata.Creator = Creator.Text;
                signOpt.Metadata.Producer = Producer.Text;
                return signOpt;
            }
        }

        public override string IntroInfo
        {
            get
            {
                return global::iPdf.Properties.Resources.SignPageIntroInfo;
            }
        }
        #endregion

        #region Ctor
        public SignPage()
        {
            actor = new PdfSigner();
        }
        #endregion

        #region Methods


        public override void OnTabPageChanged()
        {
            base.OnTabPageChanged();

            LoadCertificateSubgroup();
        }

        protected override void LoadCertificateSubgroup()
        {
            base.LoadCertificateSubgroup();
            this.SignatureGroup.SuspendLayout();
            this.SignatureGroup.Controls.Clear();
            this.SignatureGroup.Controls.Add(Certificate);
            this.SignatureGroup.Controls.Add(CertificatePassword);
            this.SignatureGroup.Controls.Add(this.CertifyPanel);
            this.SignatureGroup.Controls.Add(this.Separator);
            this.SignatureGroup.Controls.Add(this.Reason);
            this.SignatureGroup.Controls.Add(this.Contact);
            this.SignatureGroup.Controls.Add(this.Location);
            this.SignatureGroup.ResumeLayout(true);
        }

        public override void UpdatePageOnlyControls()
        {
            if (!componentsInitialized)
            {
                return;
            }

            if (Global.MainWindow.Busy || !Global.Grid.HasItems)
            {
                this.SignatureGroup.Enabled = false;
                this.MetadataGroup.Enabled = false;
                return;
            }

            this.SignatureGroup.Enabled = true;
            Certificate.Enabled = true;
            CertificatePassword.Enabled = 
                Certificate.IsValid && Certificate.IsFile;

            this.CertifyLevel.Enabled = ReadyToAct;
            this.VisibleSignature.Enabled = ReadyToAct;
            this.VisibleSignature.Checked = ReadyToAct? this.VisibleSignature.Checked : false;

            this.Reason.Enabled = ReadyToAct;
            this.Contact.Enabled = ReadyToAct;
            this.Location.Enabled = ReadyToAct;
            Helper.EnableControl(MetadataGroup, ReadyToAct);
        }

        protected override void InitializeComponent()
        {
            base.InitializeComponent();
            this.SignatureGroup = new Elegant.Ui.RibbonGroup();

            this.CertifyPanel = new Elegant.Ui.Panel();
            this.CertifyLevel = new Elegant.Ui.ComboBox();
            this.VisibleSignature = new Elegant.Ui.CheckBox();

            this.Separator = new Elegant.Ui.Separator();

            this.Reason = new iPdf.HoverSelectionTextBox();
            this.Contact = new iPdf.HoverSelectionTextBox();
            this.Location = new iPdf.HoverSelectionTextBox();

            this.MetadataGroup = new Elegant.Ui.RibbonGroup();
            this.Author = new iPdf.HoverSelectionTextBox();
            this.Title = new iPdf.HoverSelectionTextBox();
            this.Subject = new iPdf.HoverSelectionTextBox();
            this.Keywords = new iPdf.HoverSelectionTextBox();
            this.Creator = new iPdf.HoverSelectionTextBox();
            this.Producer = new iPdf.HoverSelectionTextBox();
            ((System.ComponentModel.ISupportInitialize)(this.SignatureGroup)).BeginInit();
            this.SignatureGroup.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.MetadataGroup)).BeginInit();
            this.MetadataGroup.SuspendLayout();
            this.SuspendLayout();

            this.Controls.Add(this.SignatureGroup);
            // 
            // SignatureGroup
            // 
            this.SignatureGroup.Controls.Add(this.CertifyPanel);
            this.SignatureGroup.Controls.Add(this.Separator);
            this.SignatureGroup.Controls.Add(this.Reason);
            this.SignatureGroup.Controls.Add(this.Contact);
            this.SignatureGroup.Controls.Add(this.Location);
            this.SignatureGroup.DialogLauncherButtonVisible = false;
            this.SignatureGroup.Id = "dcb4639f-70bd-4c4c-869d-35de9e9bffa7";
            this.SignatureGroup.Location = new System.Drawing.Point(4, 3);
            this.SignatureGroup.Name = "SignatureGroup";
            this.SignatureGroup.SmallImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.Certificate16)});
            this.SignatureGroup.Size = new System.Drawing.Size(398, 94);
            this.SignatureGroup.TabIndex = 0;
            this.SignatureGroup.Text = "Signature";

            // 
            // CertifyPanel
            // 
            this.CertifyPanel.Controls.Add(this.CertifyLevel);
            this.CertifyPanel.Controls.Add(this.VisibleSignature);
            this.CertifyPanel.Id = "f540f87a-c02c-46b8-9451-4934f3d27deb";
            this.CertifyPanel.Location = new System.Drawing.Point(3, 50);
            this.CertifyPanel.Name = "CertifyPanel";
            this.CertifyPanel.Size = new System.Drawing.Size(212, 23);
            this.CertifyPanel.TabIndex = 3;
            this.CertifyPanel.Text = "Certify Panel";
            // 
            // CertifyLevel
            // 
            this.CertifyLevel.BackColor = System.Drawing.SystemColors.Window;
            this.CertifyLevel.DrawMode = System.Windows.Forms.DrawMode.Normal;
            this.CertifyLevel.DroppedDown = false;
            this.CertifyLevel.Editable = false;
            this.CertifyLevel.FormatInfo = null;
            this.CertifyLevel.FormatString = "";
            this.CertifyLevel.Id = "7aafab1e-3510-4795-989c-ce418b89612a";
            this.CertifyLevel.InformativenessMinimumLevel = "Elegant.Ui.ComboBoxInformativenessLevel:ComboBoxWithLabel";
            this.CertifyLevel.Items.AddRange(certifyLevels);
            this.CertifyLevel.SelectedIndex = 0;
            this.CertifyLevel.LabelText = "Certify Level";
            this.CertifyLevel.Location = new System.Drawing.Point(1, 0);
            this.CertifyLevel.Name = "CertifyLevel";
            this.CertifyLevel.Size = new System.Drawing.Size(159, 23);
            this.CertifyLevel.Sorted = false;
            this.CertifyLevel.TabIndex = 0;
            this.CertifyLevel.TextEditorWidth = 62;
            // 
            // VisibleSignature
            // 
            this.VisibleSignature.Id = "522dd97f-e4c5-40ff-a0fb-43b9856830e0";
            this.VisibleSignature.Location = new System.Drawing.Point(160, 0);
            this.VisibleSignature.Name = "VisibleSignature";
            this.VisibleSignature.Size = new System.Drawing.Size(54, 23);
            this.VisibleSignature.TabIndex = 1;
            this.VisibleSignature.Text = "Visible";
            // 
            // Separator
            // 
            this.Separator.Id = "5ec14005-e555-4103-8305-32adb1cf1927";
            this.Separator.Location = new System.Drawing.Point(219, 7);
            this.Separator.Name = "Separator";
            this.Separator.Size = new System.Drawing.Size(2, 60);
            this.Separator.TabIndex = 4;
            // 
            // Reason
            // 
            this.Reason.Id = "34791a82-056a-4c78-b3b8-16333834bf20";
            this.Reason.InformativenessMinimumLevel = "Elegant.Ui.TextBoxInformativenessLevel:TextBoxWithLabel";
            this.Reason.LabelAreaWidthTemplate = "Location";
            this.Reason.LabelText = "Reason";
            this.Reason.Location = new System.Drawing.Point(217, 2);
            this.Reason.Name = "Reason";
            this.Reason.Size = new System.Drawing.Size(176, 24);
            this.Reason.TabIndex = 3;
            this.Reason.TextEditorWidth = 121;
            // 
            // Contact
            // 
            this.Contact.Id = "eeba8fd6-2a22-4379-9453-2f6130eeb355";
            this.Contact.InformativenessMinimumLevel = "Elegant.Ui.TextBoxInformativenessLevel:TextBoxWithLabel";
            this.Contact.LabelAreaWidthTemplate = "Location";
            this.Contact.LabelText = "Contact";
            this.Contact.Location = new System.Drawing.Point(217, 26);
            this.Contact.Name = "Contact";
            this.Contact.Size = new System.Drawing.Size(176, 24);
            this.Contact.TabIndex = 4;
            this.Contact.TextEditorWidth = 121;
            // 
            // Location
            // 
            this.Location.Id = "f5c62ad9-f08a-43b2-ab22-c0d8c4902534";
            this.Location.InformativenessMinimumLevel = "Elegant.Ui.TextBoxInformativenessLevel:TextBoxWithLabel";
            this.Location.LabelAreaWidthTemplate = "";
            this.Location.LabelText = "Location";
            this.Location.Location = new System.Drawing.Point(217, 50);
            this.Location.Name = "Location";
            this.Location.Size = new System.Drawing.Size(176, 24);
            this.Location.TabIndex = 5;
            this.Location.TextEditorWidth = 121;

            this.Controls.Add(this.MetadataGroup);
            // 
            // MetadataGroup
            // 
            this.MetadataGroup.Controls.Add(this.Author);
            this.MetadataGroup.Controls.Add(this.Title);
            this.MetadataGroup.Controls.Add(this.Subject);
            this.MetadataGroup.Controls.Add(this.Keywords);
            this.MetadataGroup.Controls.Add(this.Creator);
            this.MetadataGroup.Controls.Add(this.Producer);
            this.MetadataGroup.DialogLauncherButtonVisible = false;
            this.MetadataGroup.Id = "95292097-6216-4d5a-a9cd-7451fa5872f9";
            this.MetadataGroup.LargeImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.Metadata)});
            this.MetadataGroup.Location = new System.Drawing.Point(404, 3);
            this.MetadataGroup.Name = "MetadataGroup";
            this.MetadataGroup.SmallImages.Images.AddRange(new Elegant.Ui.ControlImage[] {
            new Elegant.Ui.ControlImage("Normal", global::iPdf.Properties.Resources.Metadata16)});
            this.MetadataGroup.Size = new System.Drawing.Size(363, 94);
            this.MetadataGroup.TabIndex = 1;
            this.MetadataGroup.Text = "Metadata";
            // 
            // Author
            // 
            this.Author.Id = "71af2a87-ae24-4355-a163-99cfda721416";
            this.Author.InformativenessMinimumLevel = "Elegant.Ui.TextBoxInformativenessLevel:TextBoxWithLabel";
            this.Author.LabelAreaWidthTemplate = "Subject";
            this.Author.LabelText = "Author";
            this.Author.Location = new System.Drawing.Point(4, 2);
            this.Author.Name = "Author";
            this.Author.Size = new System.Drawing.Size(176, 24);
            this.Author.TabIndex = 6;
            this.Author.TextEditorWidth = 128;
            // 
            // Title
            // 
            this.Title.Id = "fa524ea8-bfb1-47f6-8548-55b3869ed7e7";
            this.Title.InformativenessMinimumLevel = "Elegant.Ui.TextBoxInformativenessLevel:TextBoxWithLabel";
            this.Title.LabelAreaWidthTemplate = "Subject";
            this.Title.LabelText = "Title";
            this.Title.Location = new System.Drawing.Point(4, 26);
            this.Title.Name = "Title";
            this.Title.Size = new System.Drawing.Size(176, 24);
            this.Title.TabIndex = 7;
            this.Title.TextEditorWidth = 128;
            // 
            // Subject
            // 
            this.Subject.Id = "2130adca-8c36-4e5c-b9a7-9a01a3b17ce2";
            this.Subject.InformativenessMinimumLevel = "Elegant.Ui.TextBoxInformativenessLevel:TextBoxWithLabel";
            this.Subject.LabelAreaWidthTemplate = "";
            this.Subject.LabelText = "Subject";
            this.Subject.Location = new System.Drawing.Point(4, 50);
            this.Subject.Name = "Subject";
            this.Subject.Size = new System.Drawing.Size(176, 24);
            this.Subject.TabIndex = 8;
            this.Subject.TextEditorWidth = 128;
            // 
            // Keywords
            // 
            this.Keywords.Id = "56662e7a-c368-4d3d-8882-1dcbdd7633b0";
            this.Keywords.InformativenessMinimumLevel = "Elegant.Ui.TextBoxInformativenessLevel:TextBoxWithLabel";
            this.Keywords.LabelAreaWidthTemplate = "";
            this.Keywords.LabelText = "Keywords";
            this.Keywords.Location = new System.Drawing.Point(182, 2);
            this.Keywords.Name = "Keywords";
            this.Keywords.Size = new System.Drawing.Size(176, 24);
            this.Keywords.TabIndex = 9;
            this.Keywords.TextEditorWidth = 116;
            // 
            // Creator
            // 
            this.Creator.Id = "728de84e-551b-4907-9a28-372ffff576a9";
            this.Creator.InformativenessMinimumLevel = "Elegant.Ui.TextBoxInformativenessLevel:TextBoxWithLabel";
            this.Creator.LabelAreaWidthTemplate = "Keywords";
            this.Creator.LabelText = "Creator";
            this.Creator.Location = new System.Drawing.Point(182, 26);
            this.Creator.Name = "Creator";
            this.Creator.Size = new System.Drawing.Size(176, 24);
            this.Creator.TabIndex = 10;
            this.Creator.TextEditorWidth = 116;
            // 
            // Producer
            // 
            this.Producer.Id = "8a53b14d-82b9-4921-882b-885e62adafb7";
            this.Producer.InformativenessMinimumLevel = "Elegant.Ui.TextBoxInformativenessLevel:TextBoxWithLabel";
            this.Producer.LabelAreaWidthTemplate = "Keywords";
            this.Producer.LabelText = "Producer";
            this.Producer.Location = new System.Drawing.Point(182, 50);
            this.Producer.Name = "Producer";
            this.Producer.Size = new System.Drawing.Size(176, 24);
            this.Producer.TabIndex = 11;
            this.Producer.TextEditorWidth = 116;

            ((System.ComponentModel.ISupportInitialize)(this.SignatureGroup)).EndInit();
            this.SignatureGroup.ResumeLayout(false);
            this.SignatureGroup.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.MetadataGroup)).EndInit();
            this.MetadataGroup.ResumeLayout(false);
            this.MetadataGroup.PerformLayout();
            this.ResumeLayout(false);
        }

        public override void ImportSession(System.Xml.XmlElement sessionNode)
        {
            string certificate = sessionNode.GetAttribute(Certificate.Name);

            if(!string.IsNullOrEmpty( certificate ))
            {
                Certificate.DefaultCertificate = new CertificateDropDownItem(Certificate, certificate, 
                                    Certificate.CertificateItem_Click );
            }

            string certPassword = sessionNode.GetAttribute(CertificatePassword.Name);
            if (!string.IsNullOrEmpty(certPassword))
            {
                CertificatePassword.Text = certPassword;
            }

            SetComboBoxBySession(sessionNode, CertifyLevel);

            SetCheckBoxBySession(sessionNode, VisibleSignature);

            Reason.Text = sessionNode.GetAttribute(Reason.Name);
            Contact.Text = sessionNode.GetAttribute(Contact.Name);
            Location.Text = sessionNode.GetAttribute(Location.Name);

            //Metadata group
            Author.Text = sessionNode.GetAttribute(Author.Name);
            Title.Text = sessionNode.GetAttribute(Title.Name);
            Subject.Text = sessionNode.GetAttribute(Subject.Name);
            Keywords.Text = sessionNode.GetAttribute(Keywords.Name);
            Creator.Text = sessionNode.GetAttribute(Creator.Name);
            Producer.Text = sessionNode.GetAttribute(Producer.Name);
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.Int32.ToString")]
        public override void ExportSession(System.Xml.XmlElement sessionNode)
        {
            Certificate.RefreshMenuItems();

            if ( Certificate.DefaultCertificate != null &&
                !string.IsNullOrEmpty( Certificate.DefaultCertificate.Text) )
            {
                sessionNode.SetAttribute(Certificate.Name, Certificate.DefaultCertificate.Text);
            }

            SetXmlElementAttribute(sessionNode, CertificatePassword.Name, CertificatePassword.Text);

            SetXmlElementAttribute(sessionNode, CertifyLevel.Name, 
                                            (CertifyLevel.SelectedIndex + 1).ToString());
            SetXmlElementAttribute(sessionNode, VisibleSignature.Name,
                                            VisibleSignature.Checked.ToString());

            SetXmlElementAttribute(sessionNode, Reason.Name, Reason.Text);
            SetXmlElementAttribute(sessionNode, Contact.Name, Contact.Text);
            SetXmlElementAttribute(sessionNode, Location.Name, Location.Text);

            SetXmlElementAttribute(sessionNode, Author.Name, Author.Text);
            SetXmlElementAttribute(sessionNode, Title.Name, Title.Text);
            SetXmlElementAttribute(sessionNode, Subject.Name, Subject.Text);
            SetXmlElementAttribute(sessionNode, Keywords.Name, Keywords.Text);
            SetXmlElementAttribute(sessionNode, Creator.Name, Creator.Text);
            SetXmlElementAttribute(sessionNode, Producer.Name, Producer.Text);
        }

        #endregion 

        #region Fields
        private Elegant.Ui.RibbonGroup SignatureGroup;

        private Elegant.Ui.Panel CertifyPanel;
        private Elegant.Ui.ComboBox CertifyLevel;
        private Elegant.Ui.CheckBox VisibleSignature;

        private Elegant.Ui.Separator Separator;

        private Elegant.Ui.TextBox Reason;
        private Elegant.Ui.TextBox Contact;
        private new Elegant.Ui.TextBox Location;
        private Elegant.Ui.RibbonGroup MetadataGroup;
        private Elegant.Ui.TextBox Author;
        private Elegant.Ui.TextBox Title;
        private Elegant.Ui.TextBox Subject;
        private Elegant.Ui.TextBox Keywords;
        private Elegant.Ui.TextBox Creator;
        private Elegant.Ui.TextBox Producer;
        #endregion

    }
}
